package com.wondersgroup.app.appointment.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.wondersgroup.core.frame.BaseController;
import com.wondersgroup.core.frame.CommonStatusResult;
import com.wondersgroup.core.model.Attachment;
import com.wondersgroup.core.resultmap.DTOManufactureInfo;
import com.wondersgroup.core.resultmap.DTOProduct;
import com.wondersgroup.core.service.ProductService;
import com.wondersgroup.core.service.UtilService;
import com.wondersgroup.core.util.DataDicConstant;
import com.wondersgroup.core.util.DataDicLoader;
import com.wondersgroup.core.util.ErrorMessageConstant;
import com.wondersgroup.core.util.TraceConstant;

@Controller
@RequestMapping("/product")
public class ProductController extends BaseController{

	@Autowired
	private ProductService productService;
	@Autowired
	private UtilService utilService;

	/*
	 * 根据条形码查询产品
	 * 
	 * @param
	 */
	@RequestMapping(value = "/queryProductByBarcode", method = RequestMethod.POST)
	@ResponseBody
	public CommonStatusResult queryProductByBarcode(
			@RequestParam(value = "barcode", required = true) String barcode,HttpServletRequest request) {
		
		if (barcode != null && !barcode.equals("")) {
			DTOProduct dtoProduct = productService.findProductByBarcode(barcode);
			request.setAttribute("productType", 2003);
			if (dtoProduct != null) {
				dtoProduct.setMaterial("纯净水,食品添加剂");
			    DTOManufactureInfo info = new DTOManufactureInfo();
				String manufacture = dtoProduct.getManufacture();
			    String manufactureAddress = dtoProduct.getManufactureAddress();
			    //String productLicense=dtoProduct.getProductionLicense();
				String material = dtoProduct.getMaterial();
				Integer guaranteeValue = dtoProduct.getGuaranteeValue();
				info.setManufacture(manufacture);
				info.setManufactureAddress(manufactureAddress);
				info.setProductLicense("QSXXXX XXXX XXXX");
				info.setMaterial(material);
				info.setGuaranteeValue(guaranteeValue);
				//List<Map<Object,Object>> companyInfo=new ArrayList<Map<Object,Object>>();
//			    Map<Object,Object> map=new HashMap<Object,Object>();
//			    map.put("manufactureAddress", manufactureAddress);
//			    map.put("manufacture", manufacture);
//			    map.put("productLicense", productLicense);
//			    companyInfo.add(map);
				Integer gu=dtoProduct.getGuaranteeUnit();
				String unit = DataDicConstant.getDataDicDetailNameById(gu);	    		
				info.setUnit(unit);
				List<DTOManufactureInfo> list = new ArrayList<DTOManufactureInfo>();
				list.add(info);
				dtoProduct.setUnit(unit);
				//dtoProduct.setCompanyInfo(companyInfo);
				dtoProduct.setInfoManufacture(list);
				return CommonStatusResult.success(ErrorMessageConstant.CODE_SUCCESS, dtoProduct);
			} else {
				return CommonStatusResult.fail(ErrorMessageConstant.CODE_FAIL,null);
			}

		} else {
			return CommonStatusResult.fail(ErrorMessageConstant.CODE_FAIL, null);
		}

	}

	 @RequestMapping(value = "/test", method = RequestMethod.POST)
	 @ResponseBody
	 public CommonStatusResult uploadpic(HttpServletRequest
	 request){//HttpServletRequest request
	
	
	
	 Attachment a = null;
	 // 添加附件
	 List<MultipartFile> requestedFiles =getMultipartFilesFromRequest(request);
	 if (requestedFiles != null && requestedFiles.size() > 0) {
	 MultipartFile file = requestedFiles.get(0);
	 a = utilService.uploadAttFile(null, TraceConstant.ATT_PRODUCT, 1967,
	 file.getName(), "", new Date(), file);
	 }
	 return CommonStatusResult.success(ErrorMessageConstant.CODE_SUCCESS, 1);
	 }
	
}
